package com.rrs.client.fragment

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.core.graphics.Insets
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.google.gson.reflect.TypeToken
import com.rrs.client.MList
import com.rrs.client.R
import com.rrs.client.Response
import com.rrs.client.Utils
import com.rrs.client.adapter.GoodsAdapter
import com.rrs.client.databinding.FragmentShopBinding
import com.rrs.client.entity.Goods
import com.rrs.client.fromLocal
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient


class ShopFragment(val systemBars: Insets?) : Fragment() {

    private lateinit var binding: FragmentShopBinding
    lateinit var utils: Utils
    val client = OkHttpClient()

    @SuppressLint("InternalInsetResource")
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        utils = Utils(requireContext())

        binding = FragmentShopBinding.inflate(inflater, container, false)

        utils.setStatusBarPadding(binding.main)

        initGoodsList()

        binding.refresh.setColorSchemeColors(resources.getColor(R.color.colorPrimary, null))
        binding.refresh.setOnRefreshListener {
            initGoodsList()
            binding.refresh.isRefreshing = false
        }

        return binding.root
    }

    @OptIn(DelicateCoroutinesApi::class)
    private fun initGoodsList() {
        var res: Response<Goods>

        GlobalScope.launch(utils.globalScopeErrorHandler) {
            withContext(Dispatchers.IO) {
                val type = object : TypeToken<MList<Goods>>() {}.type
                res =
                    utils.getObjsFromCloud<Goods>(
                        client,
                        "${resources.getString(R.string.ServerUrl)}/getGoodsList",
                        type,
                        "goodsList",
                        true
                    )
            }
            withContext(Dispatchers.Main) {
                if (res.source == fromLocal) {
                    utils.sendMsg("未连接服务器，已为您加载商品缓存")
                }
                res.list.forEach { carousel ->
                    Log.d("", carousel.id.toString())
                }
                //val decoration =
                //    DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL)
                //decoration.setDrawable(
                //    ResourcesCompat.getDrawable(resources, R.drawable.divider, null)!!
                //)
                //binding.policyList.addItemDecoration(decoration)
                // 双列布局
                binding.policyList.layoutManager = StaggeredGridLayoutManager(
                    2,
                    StaggeredGridLayoutManager.VERTICAL
                )
                binding.policyList.adapter = GoodsAdapter(requireContext(), res.list)
            }
        }
    }
}